<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/29
 * Time: 17:31
 */
namespace app\admin\controller;

use app\admin\model\AuthRule;
use Think\Cache;
use Think\Db;

class Menu extends Base
{
    /**
     * 后台菜单列表
     */
    public function admin_menu_list()
    {
        $pid=input('pid',0);
        $level=input('level',0);
        $id_str=input('id','pid');
        $admin_rule=Db::name('auth_rule')->where('pid',$pid)->order('sort')->select();
        $admin_rule_all=Db::name('auth_rule')->order('sort')->select();
        $arr = menu_left($admin_rule,'id','pid','─',$pid,$level,$level*20);
        $arr_all = menu_left($admin_rule_all,'id','pid','─',0,$level,$level*20);
        $this->assign('admin_rule',$arr);
        $this->assign('admin_rule_all',$arr_all);
        $this->assign('pid',$id_str);
        if(request()->isAjax()){
            return $this->fetch('ajax_admin_rule_list');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 添加后台菜单
     */
    public function admin_menu_add(){
        if ($this->request->isPost()){
            $pid=Db::name('auth_rule')->where(array('id'=>input('pid')))->field('level')->find();
            $level=$pid['level']+1;
            $name=input('name');
            $name=AuthRule::check_name($name,$level);
            if($name){
                $sldata=array(
                    'name'=>$name,
                    'title'=>input('title'),
                    'status'=>input('status',0,'intval'),
                    'sort'=>input('sort',50,'intval'),
                    'pid'=>input('pid'),
                    'addtime'=>time(),
                    'css'=>input('css',''),
                    'level'=>$level,
                );
                Db::name('auth_rule')->insert($sldata);
                return json(['code'=>200,'msg'=>'权限添加成功']);
            }else{
                return json(['code'=>0,'msg'=>'控制器或方法不存在,或提交格式不规范']);
            }

        }else{
            $level = 0;
            $admin_rule_all=Db::name('auth_rule')->order('sort')->select();
            $arr_all = menu_left($admin_rule_all,'id','pid','─',0,$level,$level*20);
            $this->assign('admin_menu_all',$arr_all);
            //渲染模板输出
            return $this->fetch();
        }

    }

    /**
     * 后台菜单编辑
     */
    public function admin_menu_edit(){

        if ($this->request->isPost()){
            $name=input('name');
            $old_pid=input('old_pid');
            $old_level=input('old_level',0,'intval');
            $pid=input('pid');
            $level_diff=0;
            //判断是否更改了pid
            if($pid!=$old_pid){
                $level=Db::name('auth_rule')->where('id',$pid)->value('level')+1;
                $level_diff=($level>$old_level)?($level-$old_level):($old_level-$level);
            }else{
                $level=$old_level;
            }
            $name=AuthRule::check_name($name,$level);
            if($name){
                $sldata=array(
                    'id'=>input('id',1,'intval'),
                    'name'=>$name,
                    'title'=>input('title'),
                    'status'=>input('status',0,'intval'),
                    'notcheck'=>input('notcheck',0,'intval'),
                    'pid'=>input('pid',0,'intval'),
                    'css'=>input('css'),
                    'sort'=>input('sort'),
                    'level'=>$level
                );
                $rst=Db::name('auth_rule')->update($sldata);
                if($rst!==false){
                    if($pid!=$old_pid){
                        //更新子孙级菜单的level
                        $auth_rule=Db::name('auth_rule')->order('sort')->select();
                        $tree=new \Tree();
                        $tree->init($auth_rule,['parentid'=>'pid']);
                        $ids=$tree->get_childs($auth_rule,$sldata['id'],true,false);
                        if($ids){
                            if($level>$old_level){
                                Db::name('auth_rule')->where('id','in',$ids)->setInc('level',$level_diff);
                            }else{
                                Db::name('auth_rule')->where('id','in',$ids)->setDec('level',$level_diff);
                            }
                        }
                    }
                    return json(['code'=>200,'msg'=>'权限修改成功']);
                }else{
                    return json(['code'=>0,'msg'=>'权限修改失败']);
                }
            }else{
                return json(['code'=>0,'msg'=>'控制器或方法不存在,或提交格式不规范']);
            }

        }else{
            //全部规则
            $admin_rule_all=Db::name('auth_rule')->order('sort')->select();
            $arr = menu_left($admin_rule_all);
            $this->assign('admin_rule',$arr);
            //待编辑规则
            $admin_rule=Db::name('auth_rule')->where(array('id'=>input('id')))->find();
            $this->assign('rule',$admin_rule);
            return $this->fetch();
        }
    }

    /**
     * 后台菜单删除
     */
    public function admin_menu_del()
    {
        $pid=input('id','','intval');
        if (!is_numeric($pid)){
            return json(['code'=>0,'msg'=>'菜单id错误']);
        }
        $arr=Db::name('auth_rule')->select();
        $tree=new \Tree();
        $tree->init($arr,['parentid'=>'pid']);
        $arrTree=$tree->get_childs($arr,$pid,true,true);
        if($arrTree){
            $rst=Db::name('auth_rule')->where('id','in',$arrTree)->delete();
            if($rst!==false){
                return json(['code'=>200,'msg'=>'菜单删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'菜单删除失败']);
            }
        }else{
            return json(['code'=>0,'msg'=>'菜单删除失败']);
        }
    }

    /**
     * 后台菜单排序
     */
    public function admin_menu_order()
    {
        if (!$this->request->isPost()){
            return json(['code'=>0,'msg'=>'提交方式不正确']);
        }else{
            foreach ($_POST as $id => $sort){
                Db::name('auth_rule')->where(array('id' => $id ))->setField('sort' , $sort);
            }
            return json(['code'=>200,'msg'=>'排序更新成功']);
        }
    }

    /**
     * APP菜单列表
     */
    public function appmenu_list()
    {
        $pid=input('pid',0);//父类id
        $level=input('level',0); //级别
        $id_str=input('id','pid');
        $items_cate = Db::name('items_cate')->where('parent_id',$pid)->order('sort')->select();
        $arr = menu_left($items_cate,'id','parent_id','─',$pid,$level,$level*20);
        foreach ($arr as $k=>$v){
            $num = Db::name('items_cate')->where('parent_id',$v['id'])->count();
            if ($num >0){
                $arr[$k]['son'] = 1;
            }else{
                $arr[$k]['son'] = 0;
            }
        }
        $this->assign('admin_rule',$arr);
        $this->assign('pid',$id_str);
        if(request()->isAjax()){
            return $this->fetch('ajax_appmenu_list');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 添加app菜单
     */
    public function appmenu_add(){
        if ($this->request->isPost()){
            $pid=Db::name('items_cate')->where(array('id'=>input('parent_id')))->field('level')->find();
            if (empty($pid)){
                return json(['code'=>0,'msg'=>'暂不支持添加最高级']);
            }
            $level=$pid['level']+1;
            $name=input('name','');
            if($name){
                $sldata=array(
                    'name'=>$name,
                    'status'=>input('status',0,'intval'),
                    'sort'=>input('sort',50,'intval'),
                    'parent_id'=>input('parent_id'),
                    'add_time'=>time(),
                    'icon'=>input('icon',''),
                    'level'=>$level,
                );
                Db::name('items_cate')->insert($sldata);
                return json(['code'=>200,'msg'=>'菜单添加成功']);
            }else{
                return json(['code'=>0,'msg'=>'菜单名称不能为空']);
            }
        }else{
            $level = 0;
            $items_cate_all=Db::name('items_cate')->order('sort')->select();
            $arr_all = menu_left($items_cate_all,'id','parent_id','─',0,$level,$level*20);
            $this->assign('admin_menu_all',$arr_all);
            //渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * app菜单编辑
     */
    public function appmenu_edit(){

        if ($this->request->isPost()){
            $name=input('name','');
            $old_pid=input('old_pid'); //原父级id
            $old_level=input('old_level',0,'intval');//原等级
            $pid=input('pid');//新父级id
            $level_diff=0;
            if ($old_pid == 0 & $pid!=$old_pid){
                return json(['code'=>0,'msg'=>'请勿更改一级类目相关信息']);
            }
            //判断是否更改了pid
            if($pid!=$old_pid){
                $level=Db::name('items_cate')->where('id',$pid)->value('level')+1;
                $level_diff=($level>$old_level)?($level-$old_level):($old_level-$level);
            }else{
                $level=$old_level;
            }
            if($name){
                $sldata=array(
                    'id'=>input('id',1,'intval'),
                    'name'=>$name,
                    'status'=>input('status',0,'intval'),
                    'parent_id'=>input('pid',0,'intval'),
                    'icon'=>input('icon',''),
                    'sort'=>input('sort'),
                    'level'=>$level
                );
                $rst=Db::name('items_cate')->update($sldata);
                if($rst!==false){
                    if($pid!=$old_pid){
                        //更新子孙级菜单的level
                        $auth_rule=Db::name('items_cate')->order('sort')->select();
                        $tree=new \Tree();
                        $tree->init($auth_rule,['parentid'=>'parent_id']);
                        $ids=$tree->get_childs($auth_rule,$sldata['id'],true,false);
                        if($ids){
                            if($level>$old_level){
                                Db::name('items_cate')->where('id','in',$ids)->setInc('level',$level_diff);
                            }else{
                                Db::name('items_cate')->where('id','in',$ids)->setDec('level',$level_diff);
                            }
                        }
                    }
                    return json(['code'=>200,'msg'=>'权限修改成功']);
                }else{
                    return json(['code'=>0,'msg'=>'权限修改失败']);
                }
            }else{
                return json(['code'=>0,'msg'=>'控制器或方法不存在,或提交格式不规范']);
            }

        }else{
            //全部规则
            $items_cate_all=Db::name('items_cate')->order('sort')->select();
            $arr = menu_left($items_cate_all,'id','parent_id');
            $this->assign('admin_rule',$arr);
            //待编辑规则
            $items_cate=Db::name('items_cate')->where(array('id'=>input('id')))->find();
            if (!empty($items_cate['icon'])){
                $items_cate['host_icon'] = get_img_url($items_cate['icon']);
            }
            $this->assign('rule',$items_cate);
            return $this->fetch();
        }
    }

    /**
     * app菜单删除
     */
    public function appmenu_del()
    {
        $pid=input('id','','intval');
        if (!is_numeric($pid)){
            return json(['code'=>0,'msg'=>'菜单id错误']);
        }
        $arr=Db::name('items_cate')->select();
        $tree=new \Tree();
        $tree->init($arr,['parentid'=>'parent_id']);
        $arrTree=$tree->get_childs($arr,$pid,true,true);
        if($arrTree){
            $rst=Db::name('items_cate')->where('id','in',$arrTree)->delete();
            if($rst!==false){
                return json(['code'=>200,'msg'=>'菜单删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'菜单删除失败']);
            }
        }else{
            return json(['code'=>0,'msg'=>'菜单删除失败']);
        }
    }

    /**
     * app菜单排序
     */
    public function appmenu_order()
    {
        if (!$this->request->isPost()){
            return json(['code'=>0,'msg'=>'提交方式不正确']);
        }else{
            foreach ($_POST as $id => $sort){
                Db::name('items_cate')->where(array('id' => $id ))->setField('sort' , $sort);
            }
            return json(['code'=>200,'msg'=>'排序更新成功']);
        }
    }

}